//
//  RootViewController.m
//  HW1_2
//
//  Created by dllo on 15/11/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"

#warning 回收键盘第二步 签协议
@interface RootViewController () <UITextFieldDelegate>

@property (nonatomic, retain) UITextField *passwdTF;
@property (nonatomic, retain) UITextField *userTF;

@end

@implementation RootViewController

- (void)dealloc
{
    [_passwdTF release];
    [self.userTF release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
//    UIImage *image = [UIImage imageNamed:@"/Users/dllo/Desktop/DLS151034/UI1_View_Label/UI1_View_Label课后作业/HW1_2/HW1_2/e0946c9431a4cf31b8fc8568ed48d135.jpg"];
//    self.view.layer.contents = (id)image.CGImage;
    
    // 用户名
    UILabel *userL = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 60, 30)];
    // 注意1, 先铺色块, 确定好大小
//    userL.backgroundColor = [UIColor redColor];
    userL.text = @"用户名";
    [self.view addSubview:userL];
    [userL release];
    
    self.userTF = [[UITextField alloc] initWithFrame:CGRectMake(userL.frame.origin.x + userL.frame.size.width + 20, userL.frame.origin.y, [UIScreen mainScreen].bounds.size.width - (userL.frame.origin.x + userL.frame.size.width + 10) - userL.frame.origin.x, userL.frame.size.height)];
    self.userTF.placeholder = @"请输入用户名";
#warning  回收键盘第一步  设置代理人
    self.userTF.delegate = self;
    self.userTF.clearButtonMode = UITextFieldViewModeAlways;
    self.userTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.userTF];
    [_userTF release];
    
    
    // 密码
    UILabel *passwdL = [[UILabel alloc] initWithFrame:CGRectMake(userL.frame.origin.x, userL.frame.origin.y + userL.frame.size.height + 20, userL.frame.size.width, userL.frame.size.height)];
    passwdL.text = @"密码";
    [self.view addSubview:passwdL];
    [passwdL release];
    
    
    self.passwdTF = [[UITextField alloc] initWithFrame:CGRectMake(self.userTF.frame.origin.x, passwdL.frame.origin.y, self.userTF.frame.size.width, self.userTF.frame.size.height)];
    self.passwdTF.placeholder = @"请输入密码";
    self.passwdTF.delegate = self;
    self.passwdTF.clearButtonMode = UITextFieldViewModeAlways;
    self.passwdTF.borderStyle = UITextBorderStyleRoundedRect;
    self.passwdTF.secureTextEntry = YES;
    [self.view addSubview:self.passwdTF];
    [_passwdTF release];
    
    
    UIButton *loginB = [UIButton buttonWithType:UIButtonTypeCustom];
    loginB.frame = CGRectMake(userL.frame.origin.x, passwdL.frame.origin.y + passwdL.frame.size.height + 200, 50, userL.frame.size.height);
    [loginB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [loginB setTitle:@"登陆" forState:UIControlStateNormal];
//    [loginB setTitle:@"王宁" forState:UIControlStateSelected];
    
    // 前景图片与背景图片的区别:
    // 1, 前景图片保留原来图片尺寸, 背景图片填充到button尺寸
    // 2, 若有标题, 前景图片将标题挤到一边, 背景图片置于标题下面
    // 应用场景: 前景图片可以应用于图片较小但需要较大生效范围的情况
    
    // 设置前景图片
    // UIImage管理图片的类
    [loginB setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];
//    选择状态下
    [loginB setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateSelected];
    
    
    // 设置背景图片
//    [loginB setBackgroundImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];
    
    
    
    
    
    
    [loginB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.view addSubview:loginB];
    
    
    // 这节课没用到
//    UIButton *registerB = [UIButton buttonWithType:UIButtonTypeCustom];
//    registerB.frame = CGRectMake(loginB.frame.origin.x + loginB.frame.size.width + 50, loginB.frame.origin.y, loginB.frame.size.width, loginB.frame.size.height);
//    [registerB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [registerB setTitle:@"注册" forState:UIControlStateNormal];
//    [registerB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [self.view addSubview:registerB];
//    
//    UIButton *findB = [UIButton buttonWithType:UIButtonTypeCustom];
//    findB.frame = CGRectMake(registerB.frame.origin.x + registerB.frame.size.width + 50, registerB.frame.origin.y, registerB.frame.size.width + 40, loginB.frame.size.height);
//    [findB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [findB setTitle:@"找回密码" forState:UIControlStateNormal];
//    [findB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [self.view addSubview:findB];
    
    
    
    
}

//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//{
//    NSLog(@"将要开始编辑");
//    
//    // 返回NO不允许编辑
//    return YES;
//}

#warning 回收键盘第三步 实现协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"点击return按键触发");
    
#warning 回收键盘第四步 取消第一响应者
//    [textField resignFirstResponder];
    
    // 切换到密码输入框
    [self.passwdTF becomeFirstResponder];
    
    if (textField == self.userTF) {
        [self.passwdTF becomeFirstResponder];
    }
    if (textField == self.passwdTF) {
        [textField resignFirstResponder];
    }
    
    return YES;
}


- (void)buttonAction:(UIButton *)sender
{
    // button选择状态的判断依据selected属性,但此属性需要手动置位
    sender.selected = !sender.selected;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
